package com.vista.questionnaire.web.rest;

import com.codahale.metrics.annotation.Timed;
import com.vista.questionnaire.domain.Answer;
import com.vista.questionnaire.domain.Examination;
import com.vista.questionnaire.repository.AnswerRepository;
import com.vista.questionnaire.service.AnswerPOIExport;
import com.vista.questionnaire.service.ValidAndSaveAnswerService;
import com.vista.questionnaire.web.rest.errors.BadRequestAlertException;
import com.vista.questionnaire.web.rest.util.HeaderUtil;
import io.github.jhipster.web.util.ResponseUtil;
import org.json.JSONException;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Optional;

/**
 * REST controller for managing Answer.
 */
@RestController
@RequestMapping("/api")
public class AnswerResource {

    private final Logger log = LoggerFactory.getLogger(AnswerResource.class);

    private static final String ENTITY_NAME = "answer";

    private final AnswerRepository answerRepository;
    private final ValidAndSaveAnswerService validAndSaveAnswerService;

    public AnswerResource(AnswerRepository answerRepository, ValidAndSaveAnswerService validAndSaveAnswerService) {
        this.validAndSaveAnswerService = validAndSaveAnswerService;
        this.answerRepository = answerRepository;
    }

    @PostMapping("/answers")
    @Timed
    public ResponseEntity<Answer> createAnswer(@RequestBody String s) throws URISyntaxException, JSONException {
        JSONObject jsonObject= new JSONObject(s);
        Answer answer = this.validAndSaveAnswerService.save(jsonObject);
        log.debug("REST request to save Answer : {}", jsonObject);
        if (answer.getId() != null) {
            throw new BadRequestAlertException("A new answer cannot already have an ID", ENTITY_NAME, "idexists");
        }
        Answer result = answerRepository.save(answer);
        return ResponseEntity.created(new URI("/api/answers/" + result.getId()))
            .headers(HeaderUtil.createEntityCreationAlert(ENTITY_NAME, result.getId().toString()))
            .body(result);
    }

//    @PutMapping("/answers")
//    @Timed
//    public ResponseEntity<Answer> updateAnswer(@RequestBody String s) throws URISyntaxException, JSONException {
//        JSONObject jsonObject= new JSONObject(s);
//        Answer answer = this.validAndSaveAnswerService.save(jsonObject);
//        log.debug("REST request to update Answer : {}", jsonObject);
//        if (answer.getId() == null) {
//            return createAnswer(s);
//        }
//        Answer result = answerRepository.save(answer);
//        return ResponseEntity.ok()
//            .headers(HeaderUtil.createEntityUpdateAlert(ENTITY_NAME, answer.getId().toString()))
//            .body(result);
//    }


    /**
     * GET  /answers : get all the answers.
     *
     * @return the ResponseEntity with status 200 (OK) and the list of answers in body
     */
    @GetMapping("/answers")
    @Timed
    public List<Answer> getAllAnswers() {
        log.debug("REST request to get all Answers");
        return answerRepository.findAll();
    }

    /**
     * GET  /answers/:id : get the "id" answer.
     *
     * @param id the id of the answer to retrieve
     * @return the ResponseEntity with status 200 (OK) and with body the answer, or with status 404 (Not Found)
     */
    @GetMapping("/answers/{id}")
    @Timed
    public ResponseEntity<Answer> getAnswer(@PathVariable Long id) {
        log.debug("REST request to get Answer : {}", id);
        Answer answer = answerRepository.findOne(id);
        return ResponseUtil.wrapOrNotFound(Optional.ofNullable(answer));
    }

    /**
     * DELETE  /answers/:id : delete the "id" answer.
     *
     * @param id the id of the answer to delete
     * @return the ResponseEntity with status 200 (OK)
     */
    @DeleteMapping("/answers/{id}")
    @Timed
    public ResponseEntity<Void> deleteAnswer(@PathVariable Long id) {
        log.debug("REST request to delete Answer : {}", id);
        answerRepository.delete(id);
        return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert(ENTITY_NAME, id.toString())).build();
    }
}
